import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入组件
// import Login from '@/views/Login/Login'
// import Reg from '@/views/Reg/Reg'
// import Main from '../views/Main/Main.vue'
import store from '../store'
// import Home from '../views/Menu/Home//Home.vue'
// import UserInfo from '../views/Menu//User/UserInfo.vue'
// import UserAvatar from '../views/Menu/User/UserAvatar.vue'
// import UserPwd from '../views/Menu/User/UserPwd.vue'
// import ArtCate from '../views/Menu/Article/ArtCate.vue'
// import ArtList from '../views/Menu/Article/ArtList.vue'
Vue.use(VueRouter)

// src/router/index.js 路由模块
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch((err) => err)
}

const routes = [
  // 配置路由规则
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/Login')
  },
  { path: '/reg', name: 'Reg', component: () => import('@/views/Reg/Reg') },
  {
    path: '/',
    name: 'Main',
    component: () => import('../views/Main/Main.vue'),
    redirect: '/home',
    // user-pwd
    children: [
      // 首页
      {
        path: 'home',
        name: 'home',
        component: () => import('../views/Menu/Home//Home.vue')
      },
      // 用户信息
      {
        path: 'user-info',
        name: 'UserInfo',
        component: () => import('../views/Menu//User/UserInfo.vue')
      },
      // 修改头像
      {
        path: 'user-avatar',
        name: 'UserAvatar',
        component: () => import('../views/Menu/User/UserAvatar.vue')
      },
      {
        path: 'user-pwd',
        name: 'UserPwd',
        component: () => import('../views/Menu/User/UserPwd.vue')
      },
      {
        path: 'art-cate',
        name: 'ArtCate',
        component: () => import('../views/Menu/Article/ArtCate.vue')
      },
      {
        path: 'art-list',
        name: 'ArtList',
        component: () => import('../views/Menu/Article/ArtList.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && to.path !== '/reg' && !store.state.user.token) {
    Vue.prototype.$message.error('您还没有登录')
    return next('/login')
  }
  next()
})
export default router
